/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.util.TaskUtil;
/*     */ import com.zimbra.cs.imap.LiteralInfo;
/*     */ import com.zimbra.cs.server.NioUtil;
/*     */ import java.nio.ByteBuffer;
/*     */ import java.util.concurrent.Callable;
/*     */ import java.util.concurrent.TimeoutException;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestNioTcpServer
/*     */   extends TestCase
/*     */ {
/*     */   private static final String LINE = "This is a line";
/*     */   private static final int BIG_DATA_SIZE = 65536;
/*     */   
/*     */   public void testExpand()
/*     */   {
/*  35 */     ByteBuffer bb = ByteBuffer.allocate(100);
/*  36 */     bb.position(99);
/*  37 */     bb = NioUtil.expand(bb, 1001);
/*  38 */     assertTrue(bb.remaining() >= 1001);
/*     */   }
/*     */   
/*     */   public void testExpandBigData() {
/*  42 */     ByteBuffer bb = ByteBuffer.allocate(10);
/*  43 */     for (int i = 0; i < 65536; i++) {
/*  44 */       bb = NioUtil.expand(bb, 1);
/*  45 */       assertTrue(bb.hasRemaining());
/*  46 */       bb.put((byte)i);
/*     */     }
/*  48 */     assertEquals(65536, bb.position());
/*  49 */     bb.flip();
/*  50 */     for (int i = 0; i < 65536; i++) {
/*  51 */       assertEquals((byte)i, bb.get());
/*     */     }
/*     */   }
/*     */   
/*     */   public void testToString() {
/*  56 */     ByteBuffer bb = ByteBuffer.wrap("This is a line".getBytes());
/*  57 */     assertEquals("This is a line", NioUtil.toAsciiString(bb));
/*     */   }
/*     */   
/*     */   public void testGetBytes() {
/*  61 */     byte[] b = new byte[100];
/*  62 */     ByteBuffer bb = ByteBuffer.wrap(b);
/*  63 */     assertEquals(b, NioUtil.getBytes(bb));
/*  64 */     assertNotSame(b, NioUtil.getBytes(bb.put((byte)1)));
/*  65 */     bb = ByteBuffer.wrap(b, 1, b.length - 1);
/*  66 */     assertNotSame(b, NioUtil.getBytes(bb));
/*     */   }
/*     */   
/*     */   public void testLiteralInfo() throws Exception {
/*  70 */     LiteralInfo li = LiteralInfo.parse(". append {10+}");
/*  71 */     assertNotNull(li);
/*  72 */     assertEquals(10, li.getCount());
/*  73 */     assertFalse(li.isBlocking());
/*  74 */     assertNull(LiteralInfo.parse(". login foo bar}"));
/*     */     try {
/*  76 */       LiteralInfo.parse(". append {-10}");
/*  77 */       fail();
/*     */     }
/*     */     catch (IllegalArgumentException e) {}
/*     */   }
/*     */   
/*     */   public void testTaskUtil1() throws Exception {
/*  83 */     long timeout = 100L;
/*     */     try {
/*  85 */       TaskUtil.call(new Callable()
/*     */       {
/*     */         public Object call() throws InterruptedException {
/*  88 */           Thread.sleep(400L);
/*  89 */           return null; } }, 100L);
/*     */     }
/*     */     catch (TimeoutException e)
/*     */     {
/*  93 */       return;
/*     */     }
/*  95 */     throw new AssertionError("Task should have timed out");
/*     */   }
/*     */   
/*     */   public void testTaskUtil2() throws Exception {
/*  99 */     long timeout = 500L;
/*     */     try {
/* 101 */       TaskUtil.call(new Callable()
/*     */       {
/*     */ 
/* 104 */         public Object call() throws InterruptedException { return null; } }, 500L);
/*     */     }
/*     */     catch (TimeoutException e)
/*     */     {
/* 108 */       throw new AssertionError("Task should not have timed out");
/*     */     }
/*     */   }
/*     */   
/*     */   private static void assertEquals(byte[] b1, byte[] b2) {
/* 113 */     assertEquals(b1.length, b2.length);
/* 114 */     for (int i = 0; i < b1.length; i++) {
/* 115 */       assertEquals("at index " + i, b1[i], b2[i]);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestNioTcpServer.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */